package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.QuestionBank;
import com.battle.pojo.entity.QuestionBankDetails;
import com.battle.pojo.param.questionBank.*;
import com.battle.pojo.vo.questionBank.*;
import com.battle.pojo.dto.questionBank.*;
import com.battle.service.repository.in.QuestionBankDetailsRepository;
import com.battle.service.repository.in.QuestionBankRepository;
import com.battle.service.repository.in.QuestionBankTypeRepository;
import com.battle.service.service.in.QuestionBankService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：题库服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class QuestionBankServiceImpl implements QuestionBankService {

    private final QuestionBankRepository questionBankRepository;
    private final QuestionBankDetailsRepository detailsRepository;
    private final QuestionBankTypeRepository typeRepository;

    public QuestionBankServiceImpl(QuestionBankRepository questionBankRepository, QuestionBankDetailsRepository detailsRepository, QuestionBankTypeRepository typeRepository) {
        this.questionBankRepository = questionBankRepository;
        this.detailsRepository = detailsRepository;
        this.typeRepository = typeRepository;
    }


    /**
     * 创建题库
     *
     * @param param
     */
    @Override
    public void createQuestionBank(CreateQuestionBankParam param) {
        QuestionBank questionBank = new QuestionBank();
        BeanUtils.copyProperties(param, questionBank);
        Long id = IdGenerator.SNOW.generate();
        questionBank.setId(id);
        questionBankRepository.create(questionBank);
    }

    /**
     * 修改题库
     *
     * @param param
     */
    @Override
    public void editQuestionBank(EditQuestionBankParam param) {
        Long id = param.getId();
        QuestionBank questionBank = questionBankRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, questionBank);
        questionBankRepository.edit(questionBank);
    }

    /**
     * 移除题库
     *
     * @param id
     */
    @Override
    public void removeQuestionBank(Long id) {
        questionBankRepository.removeById(id);

    }

    /**
     * 启动/禁用题库
     *
     * @param id
     */
    @Override
    public void enabledQuestionBank(Long id) {
        QuestionBank questionBank = questionBankRepository.getByIdCheck(id);
        questionBank.setIsDisable(!questionBank.getIsDisable());
        questionBankRepository.edit(questionBank);
    }

    /**
     * 获取题库详情
     *
     * @param id
     * @return
     */
    @Override
    public GetQuestionBankVo getQuestionBank(Long id) {
        QuestionBank questionBank = questionBankRepository.getByIdCheck(id);
        GetQuestionBankVo vo = new GetQuestionBankVo();
        BeanUtils.copyProperties(questionBank, vo);
        return vo;
    }

    /**
     * 获取题库 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetQuestionBankListVo> getQuestionBankList(GetQuestionBankListParam param) {

        SelectQuestionBankDto dto = new SelectQuestionBankDto();
        BeanUtils.copyProperties(param, dto);
        Page<GetQuestionBankListVo> page = questionBankRepository.selectQuestionBank(dto);
        List<GetQuestionBankListVo> questionBankList = page.getRecords();
        //提取ids
        List<Long> ids = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(questionBankList)) {
            ids= questionBankList.stream().map(GetQuestionBankListVo::getId).collect(Collectors.toList());
            Map<Long, List<QuestionBankDetails>> map = detailsRepository.getMapListByBankIds(ids);
            for (GetQuestionBankListVo questionBank : questionBankList) {
                if (map.get(questionBank.getId()) != null) {
                    questionBank.setNumber(map.get(questionBank.getId()).size());
                }
            }
        }
        return new PageResult<>(questionBankList, page);

    }

    /**
     * 获取题库下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetQuestionBankDownListVo> getQuestionBankDownList(GetQuestionBankDownListParam param) {
        QuestionBank po = new QuestionBank();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<QuestionBank> questionBankList = questionBankRepository.getListByEntity(po);

        List<GetQuestionBankDownListVo> list = new ArrayList<>();
        for (QuestionBank questionBank : questionBankList) {
            GetQuestionBankDownListVo vo = new GetQuestionBankDownListVo();
            BeanUtils.copyProperties(questionBank, vo);
            list.add(vo);
        }
        return list;
    }


}



